package com.itqf.mybatis;

import com.itqf.bean.User;
import com.itqf.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * projectName: mybatis2103
 *
 * @author: 赵伟风
 * time: 2021/6/24 10:14
 * description:
 */
public class UseMybatisOneCache {

    public static void main(String[] args) throws Exception{


        //1.读取配置
        InputStream stream = Resources.getResourceAsStream("mybatis_config.xml");
        //2.创建sqlsesssionfactorybuilder
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        //3.创建sqlsessionfactory
        SqlSessionFactory sqlSessionFactory = factoryBuilder.build(stream);
        //4.创建sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //5.获取mapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.queryUser(1);

        System.out.println("user = " + user);

        sqlSession.close();

        System.out.println("------------------------");


        SqlSession sqlSessionnew = sqlSessionFactory.openSession();

        UserMapper mapper1 = sqlSessionnew.getMapper(UserMapper.class);
        User user1 = mapper1.queryUser(1);
        System.out.println("user1 = " + user1);

        sqlSessionnew.close();


        System.out.println("----------------------------");
        SqlSession sqlSessionnews = sqlSessionFactory.openSession();

        UserMapper mapper2 = sqlSessionnews.getMapper(UserMapper.class);
        User user2 = mapper2.queryUser(1);
        System.out.println("user1 = " + user2);

        sqlSessionnews.close();




    }
}
